# Maintainer: Tim Stahlhut <stahta01@gmail.com>

_realname=wxWidgets
_wx_basever=3.2
# Example _wx_buildver value is "-rc"
_wx_buildver=

pkgbase=mingw-w64-${_realname,,}${_wx_basever}
pkgname=("${MINGW_PACKAGE_PREFIX}-wxwidgets${_wx_basever}-common"
         "${MINGW_PACKAGE_PREFIX}-wxwidgets${_wx_basever}-msw-cb_headers"
         "${MINGW_PACKAGE_PREFIX}-wxwidgets${_wx_basever}-common-libs"
         "${MINGW_PACKAGE_PREFIX}-wxwidgets${_wx_basever}-msw-libs"
         "${MINGW_PACKAGE_PREFIX}-wxwidgets${_wx_basever}-gtk3-libs"
         "${MINGW_PACKAGE_PREFIX}-wxwidgets${_wx_basever}-msw"
         "${MINGW_PACKAGE_PREFIX}-wxwidgets${_wx_basever}-gtk3")
pkgver=${_wx_basever}.2.1
pkgrel=2
pkgdesc="A C++ library that lets developers create applications for Windows, Linux and UNIX (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://wxwidgets.org/"
license=('spdx:wxWindows')
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-autotools"
  "${MINGW_PACKAGE_PREFIX}-libpng"
  "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
  "${MINGW_PACKAGE_PREFIX}-libtiff"
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-gtk3"
  "${MINGW_PACKAGE_PREFIX}-pkg-config"
  "${MINGW_PACKAGE_PREFIX}-libsecret"
  "${MINGW_PACKAGE_PREFIX}-libnotify"
  "${MINGW_PACKAGE_PREFIX}-pcre2"
)
options=('strip' 'staticlibs' 'buildflags' '!debug')
#options=('!strip' 'staticlibs' 'buildflags' 'debug') # Best options for debugging CodeLite crashes
source=(
  https://github.com/wxWidgets/wxWidgets/releases/download/v${pkgver}${_wx_buildver}/${_realname}-${pkgver}${_wx_buildver}.tar.bz2

  # This patch is a MSys2 run-time patch
  001-wxWidgets-3.0.2-relocate-prefix-in-bin-wx-config.patch

  # the wxTeam rejected this patch
  005-wxWidgets-3.1.3-Remove-WX_LIBS_STATIC-from-m4.patch
)
sha256sums=('dffcb6be71296fff4b7f8840eb1b510178f57aa2eb236b20da41182009242c02'
            '2b3b183a6a76ad539abc49a41033aa923c13b395c0e55189ba962068781c7135'
            '4a4828f0c9cdc638cffde6a30b5dfb14283719acc9e89e19de8ec2d5a80a5aec')
prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}${_wx_buildver}

  # Fix MSys2 Run-Time wx-config bug.
  patch -p1 -i "${srcdir}"/001-wxWidgets-3.0.2-relocate-prefix-in-bin-wx-config.patch

  # This patch is not really needed; but, WX_LIBS_STATIC does not work correctly under MSys2
  # Removed it to see if anything breaks or if anything is fixed.
  patch -p1 -i "${srcdir}"/005-wxWidgets-3.1.3-Remove-WX_LIBS_STATIC-from-m4.patch
}

build() {
  ####
  # Configure options added to support other software:
  #   --enable-graphics_ctx     codelite
  #   --enable-graphics-d2d     codeblocks
  #
  # Configure options added to check for build issues
  #   --disable-precomp-headers
  #
  # Configure options added to avoid possible future issues
  #   --with-cxx=17
  #   --enable-stl
  #
  # Configure options added to avoid warnings:
  #   --with-regex=builtin
  #
  # Configure options known to cause build errors:
  #   --disable-regkey                                compile error
  #
  # Configure options known to cause build errors under wxGTK/Win:
  #   --with-opengl                                   configure error
  #   --enable-accessibility                          compile error
  #     "wxUSE_ACCESSIBILITY is currently only supported under wxMSW"
  #
  # Configure options added to avoid wxGTK/Win build errors:
  #   --without-opengl
  #
  ####

  if check_option "buildflags" "y"; then
  # Remove the -O and -ggdb options to avoid configuration warnings
  # from the normal settings found in /etc/makepkg_mingw??.conf

    CXXFLAGS=${CXXFLAGS/-Og }
    CXXFLAGS=${CXXFLAGS/-O2 }
    CXXFLAGS=${CXXFLAGS/-ggdb }

    CFLAGS=${CFLAGS/-Og }
    CFLAGS=${CFLAGS/-O2 }
    CFLAGS=${CFLAGS/-ggdb }
  fi

  local -a _debug_options=()

  if check_option "debug" "y"; then
    _debug_options+=("--enable-debug=yes")
    _debug_options+=("--enable-debug_gdb")
  else
    _debug_options+=("--enable-optimise")
  fi

  #echo "_debug_options := ${_debug_options[@]}"

  [[ -d "${srcdir}"/build-gtk3-${MSYSTEM}-shared ]] && rm -rf "${srcdir}"/build-gtk3-${MSYSTEM}-shared
  mkdir -p "${srcdir}"/build-gtk3-${MSYSTEM}-shared && cd "${srcdir}"/build-gtk3-${MSYSTEM}-shared

  ../${_realname}-${pkgver}${_wx_buildver}/configure \
    --prefix=${MINGW_PREFIX} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --build=${MINGW_CHOST} \
    "${_debug_options[@]}" \
    --without-xtest \
    --enable-shared \
    --enable-stl \
    --enable-iff \
    --disable-permissive \
    --enable-unicode \
    --enable-graphics_ctx \
    --enable-graphics-d2d \
    --disable-monolithic \
    --disable-precomp-headers \
    --with-gtk=3 \
    --with-cxx=17 \
    --with-libpng=sys \
    --with-libjpeg=sys \
    --with-libtiff=sys \
    --with-zlib=sys \
    --with-expat=sys \
    --with-regex=builtin \
    --with-liblzma \
    --without-opengl

  make #VERBOSE=1

  [[ -d "${srcdir}"/install-gtk3-${MSYSTEM}-shared ]] && rm -rf "${srcdir}"/install-gtk3-${MSYSTEM}-shared
  mkdir -p "${srcdir}"/install-gtk3-${MSYSTEM}-shared 

  make DESTDIR="${srcdir}/install-gtk3-${MSYSTEM}-shared" install


  [[ -d "${srcdir}"/build-gtk3-${MSYSTEM}-static ]] && rm -rf "${srcdir}"/build-gtk3-${MSYSTEM}-static
  mkdir -p "${srcdir}"/build-gtk3-${MSYSTEM}-static && cd "${srcdir}"/build-gtk3-${MSYSTEM}-static

  ../${_realname}-${pkgver}${_wx_buildver}/configure \
    --prefix=${MINGW_PREFIX} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --build=${MINGW_CHOST} \
    "${_debug_options[@]}" \
    --without-xtest \
    --disable-shared \
    --enable-stl \
    --enable-iff \
    --disable-permissive \
    --enable-unicode \
    --enable-graphics_ctx \
    --enable-graphics-d2d \
    --disable-monolithic \
    --disable-precomp-headers \
    --with-gtk=3 \
    --with-cxx=17 \
    --with-libpng=builtin \
    --with-libjpeg=builtin \
    --with-libtiff=builtin \
    --with-zlib=builtin \
    --with-expat=builtin \
    --with-regex=builtin \
    --with-liblzma \
    --without-opengl

  make #VERBOSE=1 -j1

  [[ -d "${srcdir}"/install-gtk3-${MSYSTEM}-static ]] && rm -rf "${srcdir}"/install-gtk3-${MSYSTEM}-static
  mkdir -p "${srcdir}"/install-gtk3-${MSYSTEM}-static 

  make DESTDIR="${srcdir}/install-gtk3-${MSYSTEM}-static" install


  [[ -d "${srcdir}"/build-msw-${MSYSTEM}-shared ]] && rm -rf "${srcdir}"/build-msw-${MSYSTEM}-shared
  mkdir -p "${srcdir}"/build-msw-${MSYSTEM}-shared && cd "${srcdir}"/build-msw-${MSYSTEM}-shared

  ../${_realname}-${pkgver}${_wx_buildver}/configure \
    --prefix=${MINGW_PREFIX} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --build=${MINGW_CHOST} \
    "${_debug_options[@]}" \
    --enable-shared \
    --enable-stl \
    --enable-iff \
    --disable-permissive \
    --enable-unicode \
    --enable-graphics_ctx \
    --enable-graphics-d2d \
    --enable-accessibility \
    --disable-monolithic \
    --disable-precomp-headers \
    --with-msw \
    --with-cxx=17 \
    --with-opengl \
    --with-libpng=sys \
    --with-libjpeg=sys \
    --with-libtiff=sys \
    --with-zlib=sys \
    --with-expat=sys \
    --with-regex=builtin \
    --with-liblzma

  make #VERBOSE=1

  [[ -d "${srcdir}"/install-msw-${MSYSTEM}-shared ]] && rm -rf "${srcdir}"/install-msw-${MSYSTEM}-shared
  mkdir -p "${srcdir}/install-msw-${MSYSTEM}-shared"

  make DESTDIR="${srcdir}/install-msw-${MSYSTEM}-shared" install


  [[ -d "${srcdir}"/build-msw-${MSYSTEM}-static ]] && rm -rf "${srcdir}"/build-msw-${MSYSTEM}-static
  mkdir -p "${srcdir}"/build-msw-${MSYSTEM}-static && cd "${srcdir}"/build-msw-${MSYSTEM}-static

  ../${_realname}-${pkgver}${_wx_buildver}/configure \
    --prefix=${MINGW_PREFIX} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --build=${MINGW_CHOST} \
    "${_debug_options[@]}" \
    --disable-shared \
    --enable-stl \
    --enable-iff \
    --disable-permissive \
    --enable-unicode \
    --enable-graphics_ctx \
    --enable-graphics-d2d \
    --enable-accessibility \
    --disable-monolithic \
    --disable-precomp-headers \
    --with-msw \
    --with-cxx=17 \
    --with-opengl \
    --with-libpng=builtin \
    --with-libjpeg=builtin \
    --with-libtiff=builtin \
    --with-zlib=builtin \
    --with-expat=builtin \
    --with-regex=builtin \
    --with-liblzma

  make #VERBOSE=1 -j1

  [[ -d "${srcdir}"/install-msw-${MSYSTEM}-static ]] && rm -rf "${srcdir}"/install-msw-${MSYSTEM}-static
  mkdir -p "${srcdir}/install-msw-${MSYSTEM}-static"

  make DESTDIR="${srcdir}/install-msw-${MSYSTEM}-static" install
}

check() {
  cd "${srcdir}/build-msw-${MSYSTEM}-shared/samples"    && make -k --jobs=1 || true
#  cd "${srcdir}/build-msw-${MSYSTEM}-shared/tests"      && make -k --jobs=1 || true

#  cd "${srcdir}"/build-gtk3-${MSYSTEM}-shared/samples   && make -k --jobs=1 # || true
#  cd "${srcdir}"/build-gtk3-${MSYSTEM}-shared/tests     && make -k --jobs=1 # || true
}

package_wxwidgets3.2-common-libs() {
  pkgdesc="wxBase shared libraries for wxwidgets ${_wx_basever} (mingw-w64)"
  conflicts=("${MINGW_PACKAGE_PREFIX}-wxwidgets${_wx_basever}-base-libs")
  depends=("${MINGW_PACKAGE_PREFIX}-expat"
           "${MINGW_PACKAGE_PREFIX}-pcre2"
           "${MINGW_PACKAGE_PREFIX}-libsecret")

  mkdir -p ${pkgdir}${MINGW_PREFIX}/bin
  cp -a ${srcdir}/install-msw-${MSYSTEM}-shared${MINGW_PREFIX}/bin/*.dll ${pkgdir}${MINGW_PREFIX}/bin/

  rm    "$pkgdir"${MINGW_PREFIX}/bin/wxmsw*.dll   # in msw libs package

  # License files
  cd "${srcdir}"/${_realname}-${pkgver}${_wx_buildver}/docs
  install -Dm644 preamble.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}${_wx_basever}/preamble.txt"
  install -Dm644 licence.txt  "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}${_wx_basever}/licence.txt"
  install -Dm644 licendoc.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}${_wx_basever}/licendoc.txt"
  install -Dm644 lgpl.txt     "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}${_wx_basever}/lgpl.txt"
  install -Dm644 gpl.txt      "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}${_wx_basever}/gpl.txt"
  install -Dm644 xserver.txt  "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}${_wx_basever}/xserver.txt"
}

package_wxwidgets3.2-msw() {
  pkgdesc="A C++ library that lets developers create applications for Windows, Linux and UNIX (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-wxwidgets${_wx_basever}-msw-libs"
           "${MINGW_PACKAGE_PREFIX}-wxwidgets${_wx_basever}-common")
  provides=("${MINGW_PACKAGE_PREFIX}-wxWidgets${_wx_basever}"
            "${MINGW_PACKAGE_PREFIX}-wxmsw${_wx_basever}"
            "${MINGW_PACKAGE_PREFIX}-wxconfig")
  conflicts=("${MINGW_PACKAGE_PREFIX}-wxconfig")


  mkdir -p ${pkgdir}${MINGW_PREFIX}/lib
  cp -a --recursive "${srcdir}/install-msw-${MSYSTEM}-static${MINGW_PREFIX}/lib/wx/" \
      ${pkgdir}${MINGW_PREFIX}/lib

  cp -a --recursive "${srcdir}/install-msw-${MSYSTEM}-shared${MINGW_PREFIX}/lib/wx/" \
      ${pkgdir}${MINGW_PREFIX}/lib

  mkdir -p ${pkgdir}${MINGW_PREFIX}/bin
  cp -a ${srcdir}/install-msw-${MSYSTEM}-static${MINGW_PREFIX}/bin/wx-config* ${pkgdir}${MINGW_PREFIX}/bin/
  mv ${pkgdir}${MINGW_PREFIX}/bin/wx-config{,-static}
  cp -a ${srcdir}/install-msw-${MSYSTEM}-shared${MINGW_PREFIX}/bin/wx-config* ${pkgdir}${MINGW_PREFIX}/bin/


  # Replace real full path with psuedo full path; to prevent
  # WARNING: Package contains reference to $(cygpath -m /)
  local MINGW_PREFIX_WIN=$(cygpath -am ${MINGW_PREFIX})
  sed -s "s|-L${MINGW_PREFIX_WIN}/lib|-L${MINGW_PREFIX}/lib|g" -i "${pkgdir}${MINGW_PREFIX}/bin/wx-config"
  sed -s "s|-L${MINGW_PREFIX_WIN}/lib|-L${MINGW_PREFIX}/lib|g" -i "${pkgdir}${MINGW_PREFIX}/bin/wx-config-static"
  sed -s "s|-L${MINGW_PREFIX_WIN}/lib|-L${MINGW_PREFIX}/lib|g" -i "${pkgdir}${MINGW_PREFIX}/lib/wx/config/msw-unicode-${_wx_basever}"
  sed -s "s|-L${MINGW_PREFIX_WIN}/lib|-L${MINGW_PREFIX}/lib|g" -i "${pkgdir}${MINGW_PREFIX}/lib/wx/config/msw-unicode-static-${_wx_basever}"

  # Workaround for https://github.com/wxWidgets/wxWidgets/issues/23314
  if [[ ${MINGW_CHOST} == aarch64-* ]]; then
    sed -s "s|\"-mthreads\"|\"\"|g" -i "${pkgdir}${MINGW_PREFIX}/bin/wx-config"
    sed -s "s|\"-mthreads\"|\"\"|g" -i "${pkgdir}${MINGW_PREFIX}/bin/wx-config-static"
  fi

  # create wx-config copy with version file suffix
  cp -a ${pkgdir}${MINGW_PREFIX}/bin/wx-config{,-${_wx_basever}}
}

package_wxwidgets3.2-msw-libs() {
  pkgdesc="wxMSW shared libraries for wxwidgets ${_wx_basever} (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-wxwidgets${_wx_basever}-common-libs"
           "${MINGW_PACKAGE_PREFIX}-libpng"
           "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
           "${MINGW_PACKAGE_PREFIX}-libtiff")

  mkdir -p ${pkgdir}${MINGW_PREFIX}/bin
  cp -a ${srcdir}/install-msw-${MSYSTEM}-shared${MINGW_PREFIX}/bin/*.dll ${pkgdir}${MINGW_PREFIX}/bin/

  rm    "$pkgdir"${MINGW_PREFIX}/bin/wxbase*.dll  # in base libs package
}

package_wxwidgets3.2-gtk3() {
  pkgdesc="GTK+3 implementation of wxWidgets ${_wx_basever} C++ GUI API (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-wxwidgets${_wx_basever}-gtk3-libs"
           "${MINGW_PACKAGE_PREFIX}-wxwidgets${_wx_basever}-common")

  mkdir -p ${pkgdir}${MINGW_PREFIX}/lib
  cp -a --recursive "${srcdir}/install-gtk3-${MSYSTEM}-static${MINGW_PREFIX}/lib/wx/" \
      ${pkgdir}${MINGW_PREFIX}/lib

  cp -a --recursive "${srcdir}/install-gtk3-${MSYSTEM}-shared${MINGW_PREFIX}/lib/wx/" \
      ${pkgdir}${MINGW_PREFIX}/lib

  mkdir -p ${pkgdir}${MINGW_PREFIX}/bin
  cp -a ${srcdir}/install-gtk3-${MSYSTEM}-static${MINGW_PREFIX}/bin/wx-config* ${pkgdir}${MINGW_PREFIX}/bin/
  cp -a ${srcdir}/install-gtk3-${MSYSTEM}-shared${MINGW_PREFIX}/bin/wx-config* ${pkgdir}${MINGW_PREFIX}/bin/

  mv "$pkgdir"${MINGW_PREFIX}/bin/wx-config{,-gtk3} # Conflicts with msw

  # Replace real full path with psuedo full path; to prevent
  # WARNING: Package contains reference to $(cygpath -m /)
  local MINGW_PREFIX_WIN=$(cygpath -am ${MINGW_PREFIX})
  sed -s "s|-L${MINGW_PREFIX_WIN}/lib|-L${MINGW_PREFIX}/lib|g" -i "${pkgdir}${MINGW_PREFIX}/bin/wx-config-gtk3"
  sed -s "s|-L${MINGW_PREFIX_WIN}/lib|-L${MINGW_PREFIX}/lib|g" -i "${pkgdir}${MINGW_PREFIX}/lib/wx/config/gtk3-unicode-${_wx_basever}"
  sed -s "s|-L${MINGW_PREFIX_WIN}/lib|-L${MINGW_PREFIX}/lib|g" -i "${pkgdir}${MINGW_PREFIX}/lib/wx/config/gtk3-unicode-static-${_wx_basever}"

  # create wx-config-gtk3 copy with version file suffix
  cp -a ${pkgdir}${MINGW_PREFIX}/bin/wx-config-gtk3{,-${_wx_basever}}
}

package_wxwidgets3.2-gtk3-libs() {
  pkgdesc="GTK+3 implementated shared libraries for wxwidgets ${_wx_basever} (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-wxwidgets${_wx_basever}-common-libs"
           "${MINGW_PACKAGE_PREFIX}-gtk3"
           "${MINGW_PACKAGE_PREFIX}-libnotify")

  mkdir -p ${pkgdir}${MINGW_PREFIX}/bin
  cp -a ${srcdir}/install-gtk3-${MSYSTEM}-shared${MINGW_PREFIX}/bin/*.dll ${pkgdir}${MINGW_PREFIX}/bin/

  rm    "$pkgdir"${MINGW_PREFIX}/bin/wxbase*.dll  # in base libs package
}

package_wxwidgets3.2-common() {
  pkgdesc="Static libraries and headers for wxWidgets ${_wx_basever} (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-wxwidgets${_wx_basever}-common-libs")
  provides=("${MINGW_PACKAGE_PREFIX}-wxwidgets-common")
  conflicts=("${MINGW_PACKAGE_PREFIX}-wxwidgets-common")

  mkdir -p ${pkgdir}${MINGW_PREFIX}/share
  cp -a --recursive "${srcdir}/install-msw-${MSYSTEM}-shared${MINGW_PREFIX}/share/" \
      ${pkgdir}${MINGW_PREFIX}/

  mkdir -p ${pkgdir}${MINGW_PREFIX}/include
  cp -a --recursive "${srcdir}/install-gtk3-${MSYSTEM}-shared${MINGW_PREFIX}/include/" \
      ${pkgdir}${MINGW_PREFIX}/

  mkdir -p ${pkgdir}${MINGW_PREFIX}/include
  cp -a --recursive "${srcdir}/install-msw-${MSYSTEM}-shared${MINGW_PREFIX}/include/" \
      ${pkgdir}${MINGW_PREFIX}/

  mkdir -p ${pkgdir}${MINGW_PREFIX}/bin
  cp -a ${srcdir}/install-msw-${MSYSTEM}-shared${MINGW_PREFIX}/bin/wxrc*.* ${pkgdir}${MINGW_PREFIX}/bin/

  mkdir -p ${pkgdir}${MINGW_PREFIX}/lib
  cp -a ${srcdir}/install-gtk3-${MSYSTEM}-static${MINGW_PREFIX}/lib/*.a ${pkgdir}${MINGW_PREFIX}/lib/
  cp -a ${srcdir}/install-gtk3-${MSYSTEM}-shared${MINGW_PREFIX}/lib/*.a ${pkgdir}${MINGW_PREFIX}/lib/
  cp -a ${srcdir}/install-msw-${MSYSTEM}-static${MINGW_PREFIX}/lib/*.a  ${pkgdir}${MINGW_PREFIX}/lib/
  cp -a ${srcdir}/install-msw-${MSYSTEM}-shared${MINGW_PREFIX}/lib/*.a  ${pkgdir}${MINGW_PREFIX}/lib/
}

package_wxwidgets3.2-msw-cb_headers() {
  pkgdesc="private wxWidgets ${_wx_basever} MSW headers needed by Code::Blocks (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-wxwidgets${_wx_basever}-common")

  mkdir -p ${pkgdir}${MINGW_PREFIX}/include/wx-${_wx_basever}/wx/msw/private
  cd ${pkgdir}${MINGW_PREFIX}/include/wx-${_wx_basever}/wx/msw/private
  cp ${srcdir}/${_realname}-${pkgver}${_wx_buildver}/include/wx/msw/private/comptr.h ./
  cp ${srcdir}/${_realname}-${pkgver}${_wx_buildver}/include/wx/msw/private/graphicsd2d.h ./
  cp ${srcdir}/${_realname}-${pkgver}${_wx_buildver}/include/wx/msw/private/keyboard.h ./
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
